package de.fhhannover.inform.itteam.snmpnetviz.gui;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class LoadingWindow extends JFrame {
	private static final long serialVersionUID = 7912416632499228249L;
	private JLabel mStatusLabel = new JLabel();
	
	private final ImageIcon mProcessImg = new ImageIcon(this.getClass().getResource("/gfx/snmpNetViz/loading.gif"));
	
	public LoadingWindow(String statustext) {
		this.setTitle("snmpNetViz");
		mStatusLabel.setIcon(mProcessImg);
		mStatusLabel.setForeground(Color.BLUE);
		mStatusLabel.setOpaque(true);
		mStatusLabel.setBackground(Color.WHITE);
		
		this.add(mStatusLabel);
		
		mStatusLabel.setBorder(BorderFactory.createLineBorder(Color.black));
		
		setStatusText(statustext);

		this.setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setUndecorated(true);
		
		this.setVisible(true);
		this.toFront();
	}
	
	public void setStatusText(String statustext) {
		mStatusLabel.setText(statustext);
		
		Font font = mStatusLabel.getFont();
		FontRenderContext fontRenderContext = mStatusLabel.getFontMetrics(font).getFontRenderContext();
		Rectangle2D stringBounds = font.getStringBounds(mStatusLabel.getText(), fontRenderContext);
		
		int width = new Double(stringBounds.getWidth()).intValue() + mProcessImg.getIconWidth() + 10;
		int height = mProcessImg.getIconHeight();
		
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd = env.getDefaultScreenDevice();
		DisplayMode dm = gd.getDisplayMode();
		
		Rectangle bounds = new Rectangle((dm.getWidth()-width)/2,(dm.getHeight()-height)/2,width,height);
		this.setBounds(bounds);
		
		this.validate();
	}

}
